//
//  HomeSendViewController.m
//  CarUser
//
//  Created by 王森林 on 2019/1/23.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "HomeSendViewController.h"
#import "HomeSendCell.h"
@interface HomeSendViewController ()
@property (nonatomic,strong) NSArray *listAry;
@property (nonatomic, strong) UIImageView *arrowImageView;
@property (nonatomic, strong) NSMutableArray *indexAry;
@end
#define BTNTAG  100000000
@implementation HomeSendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"配送到家";

     [self createUI];
    [self fetchDataFromNet];
    // Do any additional setup after loading the view.
}

- (void)createUI
{
     self.mainTableView.frame = CGRectMake(0, 0, BOUNDWIDTH, BOUNDHEIGHT-64);
    [self.mainTableView registerClass:[HomeSendCell class] forCellReuseIdentifier:@"HomeSendCell"];
    
}

-(void)fetchDataFromNet
{
    //收藏夹列表
//    UserRes *user = [CommonMethod getUser];
    NSDictionary *params = @{
                             @"FKEY":[CommonMethod createMD5Key:@"TOKEN"],
                             @"TOKEN":@"ALL",
                             @"showCount":@"20"
                             
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_26 params:params md5Paraname:nil success:^(id  _Nullable response) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        
        NSDictionary *resultDict  = response;
        self.listAry = [NSArray arrayWithArray:[resultDict objectForKey:@"list"]];
        [self.mainTableView reloadData];
        
        
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
    
    
}

- (void)handleBtnAction:(id)sender
{
    UIButton *btn = sender;
    NSInteger tag = btn.tag  - BTNTAG;
    if (self.indexAry.count == 0) {
        [self.indexAry addObject:[NSString stringWithFormat:@"%ld",tag]];
    } else {
        if ([self.indexAry containsObject:[NSString stringWithFormat:@"%ld",tag]]) {
            [self.indexAry removeObject:[NSString stringWithFormat:@"%ld",tag]];
        } else {
             [self.indexAry addObject:[NSString stringWithFormat:@"%ld",tag]];
            
        }
        
    }
    
    NSIndexSet *indexSet= [[NSIndexSet alloc]initWithIndex:tag];
    [self.mainTableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    
}


#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.listAry.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     NSDictionary *dict = self.listAry[section];
     NSArray *followAry = [dict objectForKey:@"followList"];
    if (followAry.count > 0 && [self.indexAry containsObject:[NSString stringWithFormat:@"%ld",section]]) {
        return 1;
    }
    return 0;
}
#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.listAry[indexPath.section];
    NSArray *followAry = [dict objectForKey:@"followList"];
    if (followAry.count > 0 && [self.indexAry containsObject:[NSString stringWithFormat:@"%ld",indexPath.section]]) {
        return 10+20*followAry.count + 10;
    }
    return 0;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 90;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSDictionary *dict = self.listAry[section];
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, BOUNDWIDTH, 20.0f)];
    headView.backgroundColor = Common_HeadTitle_Color;
   
    UILabel *orderLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, BOUNDWIDTH-15*2, 15.0f)];
    orderLabel.text = [NSString stringWithFormat:@"订单号:%@",[dict objectForKey:@"ORDERINFO_ID"]];
    orderLabel.font = FONT_SIZE(12);
    orderLabel.textColor = [UIColor whiteColor];
    [headView addSubview:orderLabel];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, BOUNDWIDTH-15*2, 15.0f)];
    contentLabel.text = [NSString stringWithFormat:@"车型内容:%@",[dict objectForKey:@"CXMC00"]];;
    contentLabel.font = FONT_SIZE(12);
    contentLabel.textColor = [UIColor whiteColor];
    [headView addSubview:contentLabel];
    
    UILabel *LogisticsLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 60, BOUNDWIDTH-15*2-200, 15.0f)];
    LogisticsLabel.text = @"当前到达:";
    LogisticsLabel.font = FONT_SIZE(12);
    LogisticsLabel.textColor = [UIColor whiteColor];
    [headView addSubview:LogisticsLabel];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame =  CGRectMake(BOUNDWIDTH-15*2-200, 60, 200, 15);
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:@"无物流信息" forState:UIControlStateNormal];
    btn.titleLabel.font = FONT_SIZE(12);
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [headView addSubview:btn];
    
    if ([self.indexAry containsObject:[NSString stringWithFormat:@"%ld",section]]) {
        NSArray *followAry = [dict objectForKey:@"followList"];
        if (followAry.count > 0) {
            UIImage *image = IMG(@"help_up");
            [btn setTitle:@"查看物流" forState:UIControlStateNormal];
            [btn setImage:image forState:UIControlStateNormal];
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
            btn.imageEdgeInsets = UIEdgeInsetsMake(0,0,0,-60);
            btn.tag  = section +BTNTAG;
            [btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        }
    } else {
        NSArray *followAry = [dict objectForKey:@"followList"];
        if (followAry.count > 0) {
            UIImage *image = IMG(@"help_down");
            [btn setTitle:@"查看物流" forState:UIControlStateNormal];
            [btn setImage:image forState:UIControlStateNormal];
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
            btn.imageEdgeInsets = UIEdgeInsetsMake(0,0,0,-60);
            btn.tag  = section +BTNTAG;
            [btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
  
    
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 89.5, BOUNDWIDTH, 0.5)];
    line.backgroundColor = [UIColor whiteColor];
    [headView addSubview:line];
    
    return headView;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeSendCell *cell = (HomeSendCell *)[tableView dequeueReusableCellWithIdentifier:@"HomeSendCell" forIndexPath:indexPath];
    NSDictionary *dict = self.listAry[indexPath.section];
     NSArray *followAry = [dict objectForKey:@"followList"];
    [cell setData:followAry];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

- (NSMutableArray *)indexAry
{
    if (!_indexAry) {
        _indexAry = [[NSMutableArray alloc] init];
    }
    return _indexAry;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
